package com.one.blocks.rbac.controller.convert;

import com.one.blocks.rbac.controller.convert.worker.BaseDomainConvert;
import com.one.blocks.rbac.controller.convert.worker.SysMenuQualifier;
import com.one.blocks.rbac.controller.convert.worker.TimeConvert;
import com.one.blocks.rbac.controller.query.SysMenuQuery;
import com.one.blocks.rbac.controller.vo.SysMenuVo;
import com.one.blocks.rbac.controller.vo.tree.SysMenuTreeNodeVo;
import com.one.blocks.rbac.domain.SysMenu;
import com.one.blocks.security.util.SubjectHelper;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

import java.util.List;

import static com.one.blocks.mvc.constant.MvcConstant.CONVERT_CREATE_BY;
import static com.one.blocks.mvc.constant.MvcConstant.CONVERT_CREATE_TIME;
import static com.one.blocks.mvc.constant.MvcConstant.CONVERT_ID;
import static com.one.blocks.mvc.constant.MvcConstant.CONVERT_UPDATE_BY;
import static com.one.blocks.mvc.constant.MvcConstant.CONVERT_UPDATE_TIME;

/**
 * @author <a href="mailto:idler41@163.com">linfuxin</a> created by 2022-06-04 16:03:55
 */
@Mapper(componentModel = "spring", uses = {SysMenuQualifier.class, TimeConvert.class}, imports = {SubjectHelper.class})
public interface SysMenuConvert extends BaseDomainConvert<SysMenu, SysMenuVo, SysMenuQuery> {

    SysMenuConvert INSTANCE = Mappers.getMapper(SysMenuConvert.class);

    /**
     * 数据对象转换
     *
     * @param v 前端参数对象
     * @return 数据对象
     */
    @Named(value = "voToEntityIgnoreId")
    @Mapping(target = CONVERT_ID, ignore = true)
    @Mapping(target = CONVERT_CREATE_BY, expression = "java(SubjectHelper.getSubjectId())")
    @Mapping(target = CONVERT_CREATE_TIME, expression = "java(SubjectHelper.getAccessTime())")
    @Mapping(target = CONVERT_UPDATE_BY, ignore = true)
    @Mapping(target = CONVERT_UPDATE_TIME, ignore = true)
    @Mapping(target = "parentId", defaultValue = "0")
    @Mapping(target = "levelNum", source = "v", qualifiedByName = "levelNum")
    @Mapping(target = "linkFlag", defaultValue = "0")
    @Mapping(target = "cacheFlag", defaultValue = "0")
    @Mapping(target = "visible", defaultValue = "0")
    @Mapping(target = "state", defaultValue = "1")
    @Override
    SysMenu voToEntityIgnoreId(SysMenuVo v);

    /**
     * 更新参数转换
     *
     * @param sysMenuVo 前端参数
     * @return 业务封装对象
     */
    @Named(value = "voToEntity")
    @Mapping(target = CONVERT_CREATE_BY, ignore = true)
    @Mapping(target = CONVERT_CREATE_TIME, ignore = true)
    @Mapping(target = CONVERT_UPDATE_BY, expression = "java(SubjectHelper.getSubjectId())")
    @Mapping(target = CONVERT_UPDATE_TIME, expression = "java(SubjectHelper.getAccessTime())")
    @Mapping(target = "parentId", defaultValue = "0")
    @Override
    SysMenu voToEntity(SysMenuVo sysMenuVo);

    SysMenuTreeNodeVo toTreeNode(SysMenu sysMenu);

    List<SysMenuTreeNodeVo> toTreeNodeList(List<SysMenu> sysMenuList);
}
